<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Main extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		//$this->auth->check_uri_permissions(true, 'backend/login');
		$this->template->build('backend/home', $this->data);
	}

	public function profile()
	{
		//$this->auth->check_uri_permissions(true, 'backend/login');

		$this->load->model('user_model');
		$this->load->model('role_model');
		$this->load->model('user_profile_model');

		$user_id = $this->auth->get_user_id();
		$this->data['info'] = $this->user_model->find(array('id' => $user_id))->row();;
		$this->data['profile'] = $this->user_profile_model->find(array('user_id' => $user_id))->row();
		$this->data['roles'] = $this->role_model->get_all()->result();

		$this->template->title('SiteName', 'Profile');
		$this->template->build('backend/profile', $this->data);
	}

	public function login()
	{
		$this->data['styles'][] = $this->data['base_url'] . 'assets/css/black/ui.css';
		$this->template->set_layout('backend_login');

		if ( ! $this->auth->is_logged_in())
		{
			$val = $this->form_validation;
			$val->set_rules('username', 'Username', 'trim|required|xss_clean');
			$val->set_rules('password', 'Password', 'trim|required|xss_clean');
			$val->set_rules('remember', 'Remember me', 'integer');


			if ($val->run() AND $this->auth->login($val->set_value('username'), $val->set_value('password'), $val->set_value('remember')))
			{
				$redirect_uri = $this->session->userdata('redirect_uri') ? $this->session->userdata('redirect_uri') : '';
				$this->session->unset_userdata('redirect_uri');

				redirect($redirect_uri);
			}
			else
			{
				if ($this->auth->is_banned())
				{
					show_error('Banned. Please contact Administrator.', 403);
				}
				else
				{
					$this->template->title('SiteName', 'Đăng nhập');
					$this->template->build('backend/login', $this->data);
				}
			}
		}
		else
		{
			redirect('backend/');
		}
	}

	public function logout()
	{
		$this->auth->logout();
		redirect('backend/login');
	}
}